//
//  LJCDiscView.m
//  WZLProduct
//
//  Created by 陆静聪 on 16/3/22.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "LJCDiscView.h"

@interface LJCDiscView ()



@end

@implementation LJCDiscView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createDisc];
    }
    return self;
}

/** 创建碟片 */
- (void)createDisc {
    // CAlayer动画
    // layer层, 负责view上的设置, 比如：颜色 内容 大小等等，view只是负责去显示layer
    
    _imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.left.offset(0);
    }];
    
    // 设置圆角
    _imageView.layer.cornerRadius = 200 / 2.0;
    // 裁掉边框
    _imageView.layer.masksToBounds = YES;
    // 加边框
    _imageView.layer.borderWidth = 3;
    _imageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    // CALayer
    // Core Animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    // 旋转的起始
    animation.fromValue = [NSNumber numberWithFloat:0];
    // 旋转的结束值
    // π 是180°;
    animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    // 重复次数
    animation.repeatCount = NSIntegerMax;
    // 是否自动回到原来位置
    // animation.autoreverses = YES;
    // 是否按照结束位置继续旋转
    animation.cumulative = YES;
    // 时间
    animation.duration = 8;

    [self.imageView.layer addAnimation:animation forKey:@"标记"];

}

- (void)getImage:(NSString *)imageUrl {
    [_imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:kStanceImage];
    [self setAnimate];
}
- (void)PlayImgStar{
    CFTimeInterval stopTime = self.imageView.layer.timeOffset;
    
    // 速度设置为1
    self.imageView.layer.speed = 1;
    //
    self.imageView.layer.beginTime = 0;
    self.imageView.layer.timeOffset = 0;
    // 设置开始的时间
    self.imageView.layer.beginTime = [self.imageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
}

/** 设置动画 */
- (void)setAnimate {
    // CALayer
    // Core Animation
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    // 旋转的起始
    animation.fromValue = [NSNumber numberWithFloat:0];
    // 旋转的结束值
    // π 是180°;
    animation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    // 重复次数
    animation.repeatCount = NSIntegerMax;
    // 是否自动回到原来位置
    // animation.autoreverses = YES;
    // 是否按照结束位置继续旋转
    animation.cumulative = YES;
    // 时间
    animation.duration = 8;
    
    [self.imageView.layer addAnimation:animation forKey:@"标记"];

}

@end
